<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" label-width="89px" v-show="showSearch" class="ele-form-search"
             @keyup.enter.native="reload"
             @submit.native.prevent>
      <!--    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="89px">-->
      <el-row :gutter="15">
        <el-col :lg="6" :md="12">
          <!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">-->
          <el-form-item label="客户名称" prop="customerName">
            <el-input
              v-model="queryParams.customerName"
              placeholder="请输入客户名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="12">
          <el-form-item label="客户等级" prop="customerLevel">
            <el-select v-model="queryParams.customerLevel" placeholder="请选择客户等级"
                       clearable
                       @keyup.enter.native="handleQuery"
            >
              <el-option label="VIP客户" value="VIP客户"></el-option>
              <el-option label="普通客户" value="普通客户"></el-option>
              <el-option label="非优先客户" value="非优先客户"></el-option>
            </el-select>
          </el-form-item>

<!--          <el-form-item label="联系电话" prop="phoneNumber">-->
<!--            <el-input-->
<!--              v-model="queryParams.phoneNumber"-->
<!--              placeholder="请输入联系电话"-->
<!--              clearable-->
<!--              @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
        </el-col>
        <el-col :lg="6" :md="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
          <!--      <el-form-item label="邮箱" prop="email">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.email"-->
          <!--          placeholder="请输入邮箱"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
        </el-col>
        <!--        <el-col :lg="6" :md="12">-->

        <!--        </el-col>-->
        <!--        <el-col :lg="8" :md="12">-->
        <!--          <el-form-item label="客户状态" prop="customerStatus">-->
        <!--            <el-select v-model="queryParams.customerStatus" placeholder="请选择客户状态"-->
        <!--                       clearable-->
        <!--                       @keyup.enter.native="handleQuery"-->
        <!--            >-->
        <!--              <el-option label="已成交" value="已成交"></el-option>-->
        <!--              <el-option label="未成交" value="未成交"></el-option>-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :lg="6" :md="12">
          <!--      <el-form-item label="创建人" prop="creater">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.creater"-->
          <!--          placeholder="请输入创建人"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
        </el-col>
        <el-col :lg="6" :md="12">

        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basicData:customerList:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basicData:customerList:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basicData:customerList:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['basicData:list:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basicData:customerList:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerListList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="客户id" align="center" prop="customerId"/>-->
      <el-table-column label="客户名称" align="center" prop="customerName" width="300" show-overflow-tooltip/>
      <el-table-column label="联系电话" align="center" prop="phoneNumber" width="200" show-overflow-tooltip/>
      <el-table-column label="邮箱" align="center" prop="email" width="200" show-overflow-tooltip/>
      <el-table-column label="客户等级" align="center" prop="customerLevel"/>
      <el-table-column label="用户状态" align="center" prop="customerStatus"/>
      <!--      <el-table-column label="客户地址" align="center" prop="customerAddress"/>-->
      <!--      <el-table-column label="创建人" align="center" prop="creater"/>-->
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basicData:customerList:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basicData:customerList:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改客户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话"
                    oninput="value=value.replace(/[^\d.]/g,'')"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
        <!--客户等级-->
        <el-form-item label="客户等级">
          <el-select v-model="form.customerLevel" placeholder="请选择客户等级">
            <el-option label="VIP客户" value="VIP客户"></el-option>
            <el-option label="普通用户" value="普通用户"></el-option>
            <el-option label="非优先客户" value="非优先客户"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户状态">
          <el-select v-model="form.customerStatus" placeholder="请选择客户等级">
            <el-option label="已成交" value="已成交"></el-option>
            <el-option label="未成交" value="未成交"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户地址" prop="customerAddress">
          <el-input v-model="form.customerAddress" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <!--        <el-form-item label="创建人" prop="creater">-->
        <!--          <el-input v-model="form.creater" placeholder="请输入创建人"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCustomerList,
  getCustomerList,
  delCustomerList,
  addCustomerList,
  updateCustomerList
} from "@/api/basicData/customerList";
// 获取当前登录状态
import {getUserProfile} from "@/api/system/user";
import {getDate} from "@/utils/getTime";
import {getToken} from "@/utils/auth";
export default {
  name: "CustomerList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      customerListList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/basicData/customerList/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        customerName: null,
        phoneNumber: null,
        email: null,
        customerLevel: null,
        customerStatus: null,
        customerAddress: null,
        creater: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phoneNumber: [
          {required: true, message: "联系电话不能为空", trigger: "blur"}
        ],
        customerName: [
          {required: true, message: "客户名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUser()
  },
  methods: {
    /** 下载模板操作 */
    importTemplate() {
      this.download('basicData/customerList/importTemplate', {
      }, `客户信息模板${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "客户管理导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      listCustomerList(this.queryParams).then(response => {
        this.customerListList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        customerId: null,
        customerName: null,
        phoneNumber: null,
        email: null,
        customerLevel: null,
        customerStatus: null,
        customerAddress: null,
        createTime: null,
        updateTime: null,
        creater: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const customerId = row.customerId || this.ids
      getCustomerList(customerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户管理";
      });
    },
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.customerId != null) {
            updateCustomerList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 新增创建人
            this.form.creater = this.user.userName
            addCustomerList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      console.log(this.ids)
      const customerIds = row.customerId || this.ids;
      this.$modal.confirm('是否确认删除客户管理编号为"' + customerIds + '"的数据项？').then(function () {
        return delCustomerList(customerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basicData/customerList/export', {
        ...this.queryParams
      }, `customerList_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
